package com.jml.Lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReenReadWriteLockTest {

    static ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();

    static Lock r = rwl.readLock();
    static Lock w = rwl.writeLock();

    public static final void get( ) throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "获取读锁");
        r.lock();
        System.out.println(Thread.currentThread().getName() + "正在做读的操作");
        Thread.sleep(100);
        r.unlock();
        System.out.println(Thread.currentThread().getName() + "释放读锁");
    }

    public static final void get1() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "获取读锁");
        r.lock();
        System.out.println(Thread.currentThread().getName() + "正在做读的操作");
        Thread.sleep(100);
        r.unlock();
        System.out.println(Thread.currentThread().getName() + "释放读锁");
    }

    public static final void get2() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "获取读锁");
        r.lock();
        System.out.println(Thread.currentThread().getName() + "正在做读的操作");
        Thread.sleep(100);
        r.unlock();
        System.out.println(Thread.currentThread().getName() + "释放读锁");
    }

    public static final void get3() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "获取读锁");
        r.lock();
        System.out.println(Thread.currentThread().getName() + "正在做读的操作");
        Thread.sleep(100);
        r.unlock();
        System.out.println(Thread.currentThread().getName() + "释放读锁");
    }

    public static final void get4() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "获取读锁");
        r.lock();
        System.out.println(Thread.currentThread().getName() + "正在做读的操作");
        Thread.sleep(100);
        r.unlock();
        System.out.println(Thread.currentThread().getName() + "释放读锁");
    }

    public static final void put() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "获取写锁");
        w.lock();
        Thread.sleep(10000);
        System.out.println(Thread.currentThread().getName() + "正在做写的操作");
        w.unlock();
        System.out.println(Thread.currentThread().getName() + "释放写锁");
    }

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ReenReadWriteLockTest.put();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ReenReadWriteLockTest.get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ReenReadWriteLockTest.get1();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ReenReadWriteLockTest.get2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ReenReadWriteLockTest.get3();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ReenReadWriteLockTest.get4();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

